﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace LiveEduWizard
{
    public class ThemeHelper : INotifyPropertyChanged
    {
        private static LinkedList<ThemeHelper> refs = new LinkedList<ThemeHelper>();
        private static Color systemAccentColor;

        public ThemeHelper()
        {
            refs.AddLast(this);
        }

        static ThemeHelper()
        {
            try
            {
                systemAccentColor = (Color)Application.Current.Resources["PhoneAccentColor"];
            }
            catch (Exception e)  //Designer do not have Application's resources
            {
                //report exception
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.StackTrace);
            }
        }

        public static void SetTheme(String background, String accentColor) 
        {   
            if (String.IsNullOrEmpty(background) || String.IsNullOrEmpty(accentColor))
            {
                return;
            }

            try
            {
                accentColor = new Regex(@"\s*").Replace(accentColor, String.Empty); 
                var dictionaries = new ResourceDictionary();
                String source = String.Format("/Theme/Resources/" + String.Format("{0}{1}", background, accentColor) + "/ThemeResources.xaml");
                var themeStyles = new ResourceDictionary { Source = new Uri(source, UriKind.Relative) };
                dictionaries.MergedDictionaries.Add(themeStyles);
                


                ResourceDictionary appResources = App.Current.Resources;
                foreach (DictionaryEntry entry in dictionaries.MergedDictionaries[0])
                {
                    SolidColorBrush colorBrush = entry.Value as SolidColorBrush;
                    SolidColorBrush existingBrush = appResources[entry.Key] as SolidColorBrush;
                    if (existingBrush != null && colorBrush != null)
                    {
                        existingBrush.Color = colorBrush.Color;
                    }
                }
                AccentColor = ((SolidColorBrush)Theme.ColorsSet.GetColorsSet()[UserSettings.AccentColorIndex].BackgroundColor).Color;
            }
            catch (Exception)
            {
            }
        }

        public static void SetSystemTheme()
        {
            String background = "light";
            if (!IsLightTheme)
            {
                background = "dark";
            }

            SetTheme(background, Theme.ColorsSet.GetColorNameByHexValue(systemAccentColor.ToString()));
            AccentColor = systemAccentColor;
        }

        public static Boolean IsLightTheme
        {
            get
            {
                return ((Visibility)(Application.Current.Resources["PhoneLightThemeVisibility"])) == Visibility.Visible;
            }
        }


        private static Image mainBackgroundImage;
        public static void SetMainBachground(Image image)
        {
            mainBackgroundImage = image;
        }

        public static void UpdateMainBackground()
        {
            if (mainBackgroundImage != null) 
            {
                mainBackgroundImage.Source = new BitmapImage(new Uri(MainBackground, UriKind.Relative));
            }
        }

        private static String MainBackground 
        {
            get 
            {
                if (String.Equals(UserSettings.BackgroundColor, "system")) 
                {
                    return ThemeHelper.IsLightTheme ? "/Images/background_light.png" : "/Images/background_dark.png";
                }
                else 
                {
                    return String.Format("/Images/background_{0}.png",UserSettings.BackgroundColor);
                }
            }
        }

        #region Buttons

        #region Back Button

        public static Uri BackButtonImage
        {
            get
            {
                return new Uri(ThemeHelper.IsLightTheme ? "Images/back_button_black.png" : "Images/back_button_white.png", UriKind.Relative);
            }
        }

        public static string BackButtonText
        {
            get
            {
                return "back";
            }
        }

        #endregion

        #region Current Button

        public static Uri CurrentButtonImage
        {
            get
            {
                return new Uri(ThemeHelper.IsLightTheme ? "Images/current_button_black.png" : "Images/current_button_white.png", UriKind.Relative);
            }
        }

        public static string CurrentButtonText
        {
            get
            {
                return "current";
            }
        }

        #endregion

        #region Switch Button

        public static Uri SwitchButtonImage
        {
            get
            {
                return new Uri(ThemeHelper.IsLightTheme ? "Images/switch_button_black.png" : "Images/switch_button_white.png", UriKind.Relative);
            }
        }

        public static string SwitchButtonText
        {
            get
            {
                return "switch";
            }
        }

        #endregion

        #region Filter Button

        public static Uri FilterButtonImage
        {
            get
            {
                return new Uri(ThemeHelper.IsLightTheme ? "Images/filter_button_black.png" : "Images/filter_button_white.png", UriKind.Relative);
            }
        }

        public static string FilterButtonText
        {
            get
            {
                return "filter";
            }
        }

        #endregion

        #region Settings Button

        public static Uri SettingsButtonImage
        {
            get
            {
                return new Uri(ThemeHelper.IsLightTheme ? "Images/settings_button_black.png" : "Images/settings_button_white.png", UriKind.Relative);
            }
        }

        public static string SettingsButtonText
        {
            get
            {
                return "settings";
            }
        }

        #endregion

        #region Select Week Button

        public static string SelectWeekButtonText
        {
            get
            {
                return "select a week";
            }
        }

        #endregion

        #region Reload Button

        public static string ReloadButtonText
        {
            get
            {
                return "reload";
            }
        }

        #endregion

        #region About Button

        public static string AboutButtonText
        {
            get
            {
                return "about";
            }
        }

        #endregion

        #endregion

        private static Color accentColor;
        public static Color AccentColor
        {
            get
            {
                return accentColor;
            }
            set
            {
                accentColor = value;
                NotifyNewTheme();
            }
        }


        public static void NotifyNewTheme()
        {
            foreach (var r in refs)
            {
                r.NotifyPropertyChanged("AccentColor");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }        
    }

    public class WPColor
    {
        public Brush BackgroundColor { get; set; }
        public string ColorTitle { get; set; }
    }  
}
